<template>
	<view class="index-box">
		
		<view v-if="productInfo && productInfo.name" class="content-box">
			<view class="title">{{productInfo.name}}</view>
			<view class="content-item">
				<view class="label">一级类目:</view>
				<view class="value">{{ productInfo.first_name }}</view>
			</view>
			<view class="content-item">
				<view class="label">二级类目:</view>
				<view class="value">{{ productInfo.second_name }}</view>
			</view>
			<view class="content-item">
				<view class="label">条形码:</view>
				<view class="value">{{ productInfo.code }}</view>
			</view>
			<view class="content-item">
				<view class="label">款式:</view>
				<view class="value">{{ productInfo.styleCode }}</view>
			</view>
			<view class="content-item">
				<view class="label">版型:</view>
				<view class="value">{{ productInfo.layout }}</view>
			</view>
			<view class="content-item">
				<view class="label">性别:</view>
				<view class="value">{{ productInfo.sex }}</view>
			</view>
			<view class="content-item">
				<view class="label">色号:</view>
				<view class="value">{{ productInfo.colorCode }}</view>
			</view>
			<view class="content-item">
				<view class="label">颜色:</view>
				<view class="value">{{ productInfo.color }}</view>
			</view>
			<view class="content-item">
				<view class="label">尺码:</view>
				<view class="value">{{ productInfo.productSize }}</view>
			</view>
			<view class="content-item">
				<view class="label">年份:</view>
				<view class="value">{{ productInfo.year }}</view>
			</view>
			<view class="content-item">
				<view class="label">季节:</view>
				<view class="value">{{ productInfo.season }}</view>
			</view>
			<view class="content-item">
				<view class="label">上市日期:</view>
				<view class="value">{{ productInfo.cityDate }}</view>
			</view>
			<view class="content-item">
				<view class="label">重量:</view>
				<view class="value">{{ productInfo.weight }}</view>
			</view>
			<view class="content-item">
				<view class="label">商品价:</view>
				<view class="value">{{ productInfo.price }}</view>
			</view>
			<view class="content-item">
				<view class="label">进货价:</view>
				<view class="value">{{ productInfo.buyingPrice }}</view>
			</view>
			<view class="content-item">
				<view class="label">库存:</view>
				<view class="value">{{ productInfo.inventory }}</view>
			</view>
			
			<view class="input-output">
				<view class="output-btn" @click="putFun('output')">出库</view>
				<view class="input-btn" @click="putFun('input')">入库</view>
			</view>
		</view>
		<view v-else class="no-product">
			输入或扫描编码查询商品信息
		</view>
		
		
		
		<view class="bottom-box" ref="wenjie">
			<view class="isbn" @click="getIsbn">
				输入69码
			</view>
			<view class="code" @click="getCode">
				扫描条形码
			</view>
		</view>
		
		<uni-popup ref="isbnpopu" type="center" backgroundColor="#fff">
			<view class="isbnpopu-box">
				<view class="title">请输入69编码</view>
				<view class="sub-title">请输入不含"-"符号的69编码</view>
				<view class="example">69编码: 6900000000001</view>
				<image class="img" src="../../static/img/isbn.png" mode="widthFix"></image>
				<view class="isbn-input">
					<input type="text" class="input" v-model="code" placeholder="请输入69编号" />
				</view>
				<view class="btn-box">
					<view class="btn cancel" @click="cancelPopu">取消</view>
					<view class="btn confirm" @click="dialogInputConfirm">确认</view>
				</view>
			</view>
			
		</uni-popup>
		
		
		<uni-popup ref="putpopu" type="center" backgroundColor="#fff">
			<view class="isbnpopu-box">
				<view class="title">{{putTypeLabel}}</view>
				<view class="isbn-input">
					<input type="text" class="input" v-model="putValue" placeholder="请输入数字" />
				</view>
				<view class="btn-box">
					<view class="btn cancel" @click="cancelPut">取消</view>
					<view class="btn confirm" @click="dialogPutConfirm">确认</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script setup>
	import { getCurrentInstance,ref } from "vue"
	import {
	    onLoad,
		onShow
	} from "@dcloudio/uni-app";
	import { showLoading, showMsg } from "./../../utils/msg.js"
	import { useUserStore } from "./../../store/index.js"
	import { getProduct, inventoryInput, inventoryOutput } from "../../configure/api/index/index.js";
	import { validatePlusInt } from "../../utils/validate.js";
	

	const userStore = useUserStore();
	
	const { proxy } = getCurrentInstance();
	const code = ref('');
	
	const delIsbn = () => {
		code.value = ''
	}

	const getIsbn = () => {
		proxy.$refs['isbnpopu'].open();
	}
	
	const cancelPopu = () => {
		code.value = ''
		proxy.$refs['isbnpopu'].close();
	}
	
	const getCode = () => {
		uni.navigateTo({
		    url: '/pages/index/mumuCode/mumuCode'
		}); 
	}

	const dialogInputConfirm = async () => {
		if(code.value){
			getProductFun(code.value)
			proxy.$refs['isbnpopu'].close();
		}else{
			showMsg("编码不能为空!")
		}
		
	}
	
	const productInfo = ref({})
	const getProductFun = () => {
		showLoading()
		getProduct({code: code.value}).then((res)=>{
			if(res && res.length){
				console.log(res[0])
				productInfo.value = res[0]
			}else{
				showMsg("商品不存在!")
			}
		}).finally(()=>{
			uni.hideLoading();
		})
	}

	const putType = ref('input')
	const putTypeLabel = ref('')
	const putValue = ref(1)
	const putFun = (type) => {
		putType.value = type
		putTypeLabel.value = type == 'input' ? '入库' : '出库';
		proxy.$refs['putpopu'].open();
	}
	
	const cancelPut = () => {
		proxy.$refs['putpopu'].close();
	}
	const dialogPutConfirm = () => {
		if(!validatePlusInt(putValue.value)){
			showMsg(`${putTypeLabel.value}数字不正确!`)
			return
		}
		uni.showModal({
		    title: '提示',
		    content: '确定变更库存?',
		    success: async function (res) {
		        if (res.confirm) {
					showLoading()
					
					if(putType.value == 'input'){
						inventoryInput({id: productInfo.value.id,inventory: putValue.value}).then(()=>{
							showMsg("操作成功!");
							getProductFun();
							proxy.$refs['putpopu'].close();
						}).finally(()=>{
							uni.hideLoading();
						})
					}else{
						inventoryOutput({id: productInfo.value.id,inventory: putValue.value}).then(()=>{
							showMsg("操作成功!");
							getProductFun();
							proxy.$refs['putpopu'].close();
						}).finally(()=>{
							uni.hideLoading();
						})
					}
					
		        } else if (res.cancel) {
		            
		        }
		    }
		});
	}

	
	onShow(()=>{
		uni.$once('mumuCode',function(data){
			if(data && data.code){
				code.value = data.code
				getProductFun()
			}else{
				code.value = ''
			}
		})
	})
	
	onLoad((detail)=>{ 
	})
	
	
</script>

<style lang="scss">
	.index-box {
		width: 100%;
		height: 100%;
		overflow-y: auto;
		padding: 20rpx 20rpx 140rpx 20rpx;
		box-sizing: border-box;
		background-repeat: no-repeat;
		background-size: 100%;
		
		.content-box{
			width: 100%;
			.title{
				font-weight: bold;
				font-size: 32rpx;
				margin-bottom: 20rpx;
			}
			
			.content-item{
				display: flex;
				justify-content: space-between;
				align-items: center;
				font-size: 27rpx;
				margin-bottom: 20rpx;
			}
			
			.input-output{
				display: flex;
				align-items: center;
				justify-content: flex-end;
				margin-top: 40rpx;
				
				.input-btn,.output-btn{
					background-color: #ebeff2;
					border-radius: 8rpx;
					margin-left: 15rpx;
					width: 120rpx;
					height: 60rpx;
					display: flex;
					justify-content: center;
					align-items: center;
					font-size: 30rpx;
				}
			}
		}
		
		.no-product{
			display: flex;
			justify-content: center;
			align-items: center;
			font-size: 30rpx;
		}
		
		.bottom-box{
			width: 100%;
			height: 120rpx;
			padding: 0 40rpx;
			box-sizing: border-box;
			@include flex(row,space-between,center);
			background-color: #fff;
			position: absolute;
			left:0;
			right:0;
			bottom: 0;
			.isbn,.code{
				height: 80rpx;
				width: 300rpx;
				box-sizing: border-box;
				@include flex();
			}
			
			.isbn{
				border: 1px solid var(--mainColor);
				border-radius: 200rpx;
				color: var(--mainColor);
			}
			
			.code{
				color: #fff;
				background-color: var(--mainColor);
				border-radius: 200rpx;
			}
		}
		
		.uni-popup__wrapper{
			background-color: transparent!important;
		}
		.isbnpopu-box{
			padding-top: 30rpx;
			width: 670rpx;
			border-radius: 30rpx;
			background-color: #fff;
			overflow: hidden;
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: space-between;
			.title{
				font-size: 32rpx;
				font-weight: 600;
				margin-bottom: 10rpx;
			}
			.sub-title{
				font-size: 24rpx;
				color: var(--borderColor);
				margin-bottom: 50rpx;
				color: #999999;
			}
			.example{
				font-size: 22rpx;
				margin-bottom: 30rpx;
			}
			.img{
				width: 230rpx;
				margin-bottom: 20rpx;
			}
			.isbn-input{
				width: 100%;
				padding: 30rpx;
				box-sizing: border-box;
				
				.input{
					width: 100%;
					height: 100rpx;
					padding: 0 20rpx;
					box-sizing: border-box;
					background-color: #f7f7f9;
					border-radius: 20rpx;
					color: #999999;
					font-size: 28rpx;
				}
			}
			.btn-box{
				width: 100%;
				display: flex;
				align-items: center;
				border-top: 1px solid var(--borderColor);
				.btn{
					width: 50%;
					height: 90rpx;
					box-sizing: border-box;
					display: flex;
					justify-content: center;
					align-items: center;
					font-size: 30rpx;
				}
				.cancel{
					border-right: 1px solid var(--borderColor);
				}
				.confirm{
					color: var(--successColor);
				}
			}
		}
		
	}
</style>
